package com.motu.monstercity.module.common.utility;

import com.motu.vertx.module.utility.toolset.TimeUtils;
import com.motu.vertx.module.utility.toolset.Tool;
import io.vertx.core.json.JsonArray;
import io.vertx.core.json.JsonObject;

public class Notice extends NoticeModel {

    public static final int NOTICE_TYPE_GAME = 0;//游戏内公告
    public static final int NOTICE_TYPE_HOME_PAGE = 1;//登录页公告
    public static final int NOTICE_TYPE_ALL = 2;//全部（游戏内外都显示）
    public static final int NOTICE_TYPE_MAINTAIN = 3;//维护公告

    private JsonArray titleList = new JsonArray();
    private JsonArray contentList = new JsonArray();

    public JsonArray getTitleList() {
        return titleList;
    }

    public JsonArray getContentList() {
        return contentList;
    }

    public Notice() {

    }

    public Notice(JsonObject obj) {
        super(obj);
        if (!Tool.isEmpty(getTitle())) {
            titleList = new JsonArray(getTitle());
        }
        if (!Tool.isEmpty(getContent2())) {
            contentList =new JsonArray(getContent2());
        }
    }

    public void putId(long id) {
        this.id = id;
    }

    public void countHash() {
        String str = "" + id + this.platform + this.type + this.title/*+this.content1*/ + this.content2 +
                this.getStartTime() + this.getEndTime() + this.sort + this.status + this.getScope() + this.getScopeType();
        this.hashCode = str.hashCode();
    }

    /**
     * 公告是否开启
     */
    public boolean isStart(int platform, int serverId, int language) {
        String title = getWordByLanguage(titleList, language);
        String content = getWordByLanguage(contentList, language);
        if (title == null || content == null) {//如果没设置这个语言的，就不下发公告
            return false;
        }
        if (this.getType() == NOTICE_TYPE_GAME) {
            try {
                if (this.getScopeType() == 1) {//包含服务器列表
                    JsonArray array = new JsonArray(this.getScope());
                    if (array.size() < 2) {
                        return false;
                    }
                    int minServerId = array.getInteger(0);
                    int maxServerId = array.getInteger(1);
                    if (serverId < minServerId || serverId > maxServerId) {
                        return false;
                    }
                }
                if (this.getScopeType() == 2) {//排除服务器列表
                    JsonArray array = new JsonArray(this.getScope());
                    if (Tool.isInList(array, serverId)) {
                        return false;
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();//服务器列表填的不是json格式
                return false;
            }
        }
        // 判断本渠道是否有公告
        boolean contain = false;// 是否包含
        if (getPlatform() > 0) {
            contain = platform == getPlatform();
        } else {
            contain = true;
        }
        if (contain) {
            long curTime = TimeUtils.getCurTime();
            if (curTime >= getStartTime() && curTime <= getEndTime()) {
                return true;
            }
        }
        return false;
    }

    public static String getWordByLanguage(JsonArray array, int language) {

        for (int i = 0, size = array.size(); i < size; i++) {
            JsonObject obj = array.getJsonObject(i);
            if (obj.containsKey("type") && obj.containsKey("word")) {
                if (obj.getInteger("type") == language) {
                    return obj.getString("word");
                }
            }
        }
        return null;
    }
}
